import EventCenter from "../../lib/EventCenter";
import Sdk from "../../lib/Sdk";
import { TipManager } from "../../lib/TipManager";
import {ui} from "../../ui/layaMaxUI"
import EventEunm from "../data/EventEunm";
import ItemBaseData from "../data/ItemBaseData";
import PlayerData from "../data/PlayerData";
export default class ShopDialog extends ui.ShopDialogUI{
    private mdata:ItemBaseData;
    private mGold:number=100;
    onOpened(_data:ItemBaseData=null){
        this.mdata=_data;
        let tnum=1;
        if(this.mdata){
            this.icon.skin=this.mdata.icon;
            this.goldBtn.getChildByName("numTxt")["text"]=this.mdata.price;
        }else{
            tnum=this.mGold;
            this.icon.skin="main/big-coin-1.png";
        }
        this.goldBtn.visible=this.mdata?true:false;
        this.numTxt.text=""+tnum;
        this.goldBtn.on(Laya.Event.CLICK,this,this.goldClick);
        this.videoBtn.on(Laya.Event.CLICK,this,this.videoClick);
        this.closeBtn.on(Laya.Event.CLICK,this,this.close);
        Sdk.getIns().showBanner();
    }
    private goldClick(){
        if(this.mdata){
            if(PlayerData.getIns().gold>=this.mdata.price){
                PlayerData.getIns().addItem(this.mdata.id+"",1);
                PlayerData.getIns().addGold(-this.mdata.price);
                EventCenter.getIns().event(EventEunm.itemChange);
                EventCenter.getIns().event(EventEunm.goldChange);
                this.close();
            }else{
                TipManager.ins.showMsgAlert("金币不足",this);
            }
        }
    }

    private videoClick(){
        Sdk.getIns().showAd((_code:number)=>{
            if(_code==0){
                if(this.mdata){
                    PlayerData.getIns().addItem(this.mdata.id+"",1);
                    PlayerData.getIns().addItemVideotimes();
                    EventCenter.getIns().event(EventEunm.itemChange);
                }else{
                    PlayerData.getIns().addGold(this.mGold);
                    PlayerData.getIns().addGoldVideoTimes();
                    EventCenter.getIns().event(EventEunm.goldChange);
                }
                this.close();
            }else{
                TipManager.ins.showMsgAlert("观看完整视频才能获取奖励",this);
            }
        });
    }

    onDisable(){
        this.goldBtn.off(Laya.Event.CLICK,this,this.goldClick);
        this.videoBtn.off(Laya.Event.CLICK,this,this.goldClick);
        this.closeBtn.off(Laya.Event.CLICK,this,this.close);
        Sdk.getIns().hideBanner();
    }
}